# -*- coding: utf-8 -*-
"""
 ----------------------------------------
|File Name:     redis_manage_file
|Author:        WYT
|date:          2020/6/9
 ----------------------------------------
|  Change Activity:
|                   2020/6/9:
-----------------------------------------
"""
import threading
import time

from redis import StrictRedis, ConnectionPool, Redis
from redis.exceptions import ConnectionError



# TODO 存入的数据 格式
# TODO operate 有 split 和 delete
from settings import redis_host, redis_port, redis_db, redis_password, web_type

a = \
{"biz_code": "", "co_id": "", "operate": "", "file_path": "", "file_save_path": ""}

class redisManageFile():
    _instance = None
    # _instance_lock = threading.Lock()
    pool = None
    # pool = ConnectionPool(host=redis_host, port=redis_port, db=redis_db, password=redis_password)
    # pool = ConnectionPool(host=redis_host, port=redis_port, db=redis_db)
    error = ConnectionError
    conn = None
    def __init__(self):
        # if self.pool is None:
        #     self.pool = ConnectionPool(host=redis_host, port=redis_port, db=redis_db, password=redis_password)
        # if self.conn is not None:
        #     self.redis = self.conn
        # else:
        #     self.conn = Redis(connection_pool=self.pool, socket_connect_timeout=60)
        #     self.redis = self.conn
        self.redis = StrictRedis(host=redis_host, port=redis_port, db=redis_db, password=redis_password, decode_responses=True, socket_connect_timeout=60)

    def handle_key(self, key):
        if web_type not in key:
            return "{}_{}".format(key, web_type)
        return key

    def list_put(self, list_key, list_value):
        list_key = self.handle_key(key=list_key)
        count = None
        # try:
        res = self.redis
        redis_value = res.lrange(list_key, 0, -1)
        if list_value not in redis_value:
            count = res.lpush(list_key, list_value)
        # except ConnectionError as err:
        #     print("redis 未启动")
        self.redis.close()
        return count

    def list_get(self, list_key):
        list_key = self.handle_key(key=list_key)
        # print(list_key)
        res = self.redis.rpop(list_key)
        self.redis.close()

        return res

if __name__ == '__main__':
    red = redisManageFile()
    res = red.redis.lrange("192.168.1.15_ctp", 0, -1)
    print(res)

    try:
        red.list_put("123", "2")
    except (ConnectionError) as err:
        print("redis 未启动")

    # res = None
    # try:
    #
    #     res = red.list_get("123")
    # except (ConnectionError) as err:
    #     print("redis 未启动")
    # if res is not None:
    #     print("a")
    # print(res)
    # print(res.decode("utf-8"))












